package com.zb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Strings;
import com.zb.dto.TbUserDto;
import com.zb.dto.UserPageDto;
import com.zb.entity.TbUserModel;
import com.zb.service.TbUserService;
import com.zb.util.PageDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName TbUserController
 * @Description 用户表控制器
 * @Author zj
 * @Date 2022/10/14 14:21
 **/
@RestController
@RequestMapping("/tb-user-model")
public class TbUserController {

    @Autowired
    public TbUserService tbUserModelService;


    @Autowired
    private KafkaTemplate kafkaTemplate;

    @GetMapping("/test")
    public String  test(){
        System.out.println("省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号省略号");
        return "模拟";
    }

    @GetMapping("/findname/{name}")
    public TbUserDto findName(@PathVariable("name") String name) {
        TbUserDto dto = new TbUserDto();
        LambdaQueryWrapper<TbUserModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbUserModel::getUsername, name);
        TbUserModel userModel = tbUserModelService.getOne(queryWrapper);
        BeanUtils.copyProperties(userModel, dto);
        return dto;
    }

    @PostMapping("/pageUser")
    public PageDto<TbUserDto> pageUser(@RequestBody UserPageDto userPageDto) {
        LambdaQueryWrapper<TbUserModel> queryWrapper = new LambdaQueryWrapper<>();
        if (!Strings.isNullOrEmpty(userPageDto.getUsername())) {
            queryWrapper.like(TbUserModel::getUsername, userPageDto.getUsername());
        }
        if (!Strings.isNullOrEmpty(userPageDto.getPhone())) {
            queryWrapper.eq(TbUserModel::getPhone, userPageDto.getPhone());
        }
        IPage<TbUserModel> page = new Page<>(userPageDto.getIndex(), userPageDto.getSize());
        tbUserModelService.page(page, queryWrapper);

        PageDto<TbUserDto> pageDto = new PageDto<>();
        List<TbUserDto> tbUserDtoList = page.getRecords().stream().map(item -> {
            TbUserDto tbUserDto = new TbUserDto();
            BeanUtils.copyProperties(item, tbUserDto);
            return tbUserDto;
        }).collect(Collectors.toList());
        pageDto.setCurrent(userPageDto.getIndex());
        pageDto.setSize(userPageDto.getSize());
        BeanUtils.copyProperties(page, pageDto);
        pageDto.setRecords(tbUserDtoList);
        return pageDto;
    }


    @PostMapping("/saveOrUpdate")
    public Integer saveOrUpdate(@RequestBody TbUserDto tbUserDto) {
        TbUserModel tbUserModel = new TbUserModel();
        BeanUtils.copyProperties(tbUserDto, tbUserModel);
        return tbUserModelService.saveOrUpdate(tbUserModel) ? 1 : 0;
    }

    @GetMapping("/info/{id}")
    public TbUserDto info(@PathVariable("id") Integer id) {
        kafkaTemplate.send("user", "进入用户详情查询,用户编号：" + id);
        TbUserModel byId = tbUserModelService.getById(id);
        TbUserDto tbUserDto = new TbUserDto();
        BeanUtils.copyProperties(byId, tbUserDto);
        kafkaTemplate.send("user", "查询到的数据:" + tbUserDto);
        return tbUserDto;
    }

    @DeleteMapping("/delete/{id}")
    public Integer delete(@PathVariable("id") Integer id) {
        return tbUserModelService.removeById(id) ? 1 : 0;
    }


}
